const mysql = require('mysql');

const connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'mapcache'
});

connection.connect();
function get(reqArry, response, request) {
    let type = reqArry[1];
    let zoom = reqArry[2];
    let x = reqArry[3];
    let y = reqArry[4];

    //console.log(  `echo >>  select Tile from gmapnetcache where Type=${type} and Zoom=${zoom} and X=${x} and Y=${y}`   );
    console.log(  " select Tile from gmapnetcache where Type="+type+" and Zoom="+zoom+" and X="+x+" and Y="+y+" "   );

    connection.query(`select Tile from gmapnetcache where Type=${type} and Zoom=${zoom} and X=${x} and Y=${y}`, function (error, results, fields) {


        if (error || !results || !results[0]) {


            response.statusCode = 500;
            response.setHeader("Content-Type", "text/plain");
            response.write(error + "\n");

    console.error(   error   );

            response.end();
            return;
        };


    console.log(   "ok ... "  );

        response.statusCode = 200;
        response.setHeader('Content-Type', 'image/png');
        response.write(results[0].Tile, "binary");
        response.end();
    });
}

exports.get = get;
